"""
绘制轮廓的最小外接圆
"""
import cv2
import numpy as np

img = cv2.imread('../data/cloud.png')
cv2.imshow('img',img)

# 灰度化
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# 二值化
t,binary = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY)
cv2.imshow('binary',binary)
# 查找轮廓
cnts,hie = cv2.findContours(binary,
                            cv2.RETR_LIST, # 查找所有轮廓
                            cv2.CHAIN_APPROX_NONE)

center,radius = cv2.minEnclosingCircle(cnts[0])
center = (int(center[0]),int(center[1]))
radius = int(radius)
# print(center)
# print(radius)

# 画最小外接圆，圆心和半径需要为整数
cv2.circle(img,
           center,
           radius,
           (0,0,255),
           2)

cv2.imshow('res',img)

cv2.waitKey()
cv2.destroyAllWindows()